package com.alibaba.dingtalk.openapi.helper;

import com.alibaba.dingtalk.openapi.exception.OApiException;
import com.alibaba.dingtalk.openapi.exception.OApiResultException;
import com.alibaba.dingtalk.openapi.utils.HttpHelper;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 注册事件回调相关接口
 * 
 * @author kaka
 */
public class EventChangeHelper {

	private static final Logger LOG = LoggerFactory.getLogger(EventChangeHelper.class);

	/**
	 * 注册事件回调接口
	 */
	public static String registerEventChange(String accessToken, List<String> callBackTag, String token, String aesKey,
                                             String url) throws OApiException {
		String signUpUrl = Env.OAPI_HOST + "/call_back/register_call_back?" + "access_token=" + accessToken;
		JSONObject args = new JSONObject();
		args.put("call_back_tag", callBackTag);
		args.put("token", token);
		args.put("aes_key", aesKey);
		args.put("url", url);

		JSONObject response = HttpHelper.httpPost(signUpUrl, args);
		LOG.info("registerEventChange : " + response);
		if (response.containsKey("errcode")) {
			return response.getString("errcode");
		} else if(response.isEmpty()) {
			//兼容现在没有回复的情况 TODO 等钉钉处理好这边的逻辑后修复
			LOG.info("/call_back/register_call_back 返回了 空， 没有errcode");
			return "1";
		} else {
			throw new OApiResultException("errcode");
		}
	}

	/**
	 * 注册互动卡片回调地址
	 */
	public static String registerInteractiveCardCallback(String accessToken, String suiteSecret, String url, String callbackRouteKey, Boolean forceUpdate) throws OApiException {
		String signUpUrl = Env.OAPI_HOST + "/topapi/im/chat/scencegroup/interactivecard/callback/register?" + "access_token=" + accessToken;
		JSONObject args = new JSONObject();
		args.put("api_secret", suiteSecret);
		args.put("callback_url", url);
		args.put("callbackRouteKey", callbackRouteKey);
		if (forceUpdate != null && forceUpdate) {
			args.put("forceUpdate", true);
		}

		JSONObject response = HttpHelper.httpPost(signUpUrl, args);
		LOG.info("registerEventChange : " + response);
		if (response.containsKey("errcode")) {
			return response.getString("errcode");
		} else if(response.isEmpty()) {
			//兼容现在没有回复的情况 TODO 等钉钉处理好这边的逻辑后修复
			LOG.info("/topapi/im/chat/scencegroup/interactivecard/callback/register 返回了 空， 没有errcode");
			return "1";
		}

		return "0";
	}

	/**
	 * 查询事件回调接口
	 */
	public static String getEventChange(String accessToken) throws OApiException {
		String url = Env.OAPI_HOST + "/call_back/get_call_back?" + "access_token=" + accessToken;
		JSONObject response = HttpHelper.httpGet(url);
		return response.toString();
	}

	/**
	 * 更新事件回调接口
	 */
	public static String updateEventChange(String accessToken, List<String> callBackTag, String token, String aesKey,
                                           String url) throws OApiException {
		String signUpUrl = Env.OAPI_HOST + "/call_back/update_call_back?" + "access_token=" + accessToken;
		JSONObject args = new JSONObject();
		args.put("call_back_tag", callBackTag);
		args.put("token", token);
		args.put("aes_key", aesKey);
		args.put("url", url);

		JSONObject response = HttpHelper.httpPost(signUpUrl, args);
		if (response.containsKey("errcode")) {
			return response.getString("errcode");
		} else {
			throw new OApiResultException("errcode");
		}
	}

	/**
	 * 删除事件回调接口
	 */
	public static String deleteEventChange(String accessToken) throws OApiException {
		String url = Env.OAPI_HOST + "/call_back/delete_call_back?" + "access_token=" + accessToken;
		JSONObject response = HttpHelper.httpGet(url);
		return response.toString();
	}

	public static String getFailedResult(String accessToken) throws OApiException {
		String url = Env.OAPI_HOST + "/call_back/get_call_back_failed_result?" + "access_token=" + accessToken;
		JSONObject response = HttpHelper.httpGet(url);
		return response.toString();
	}

}
